const Router = require('@koa/router')
const proxy = require('koa-better-http-proxy')

const router = new Router()
/**
 * 视频资源转发
 */
const handleProxy = proxy('http://mpvideo.qpic.cn', {
  // 转发路径处理
  proxyReqPathResolver(ctx) {
    // 去掉/video
    return ctx.url.substring(6)
  },
  // 修改请求头
  proxyReqOptDecorator(proxyReqOpts) {
    // 添加或修改请求头
    proxyReqOpts.headers.Host = 'mpvideo.qpic.cn'
    proxyReqOpts.headers.Referer = 'https://mp.weixin.qq.com'
    proxyReqOpts.headers['User-Agent']
        = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36'
    return proxyReqOpts
  }
})

router.get(/^\/video/, handleProxy)

module.exports = router.routes()
